package com.foo.web.controller.wechat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.foo.wechat.domain.WxFarm;
import com.foo.wechat.domain.WxUser;
import com.foo.wechat.service.IWxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxCanteen;
import com.foo.wechat.service.IWxCanteenService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;
import com.foo.common.utils.poi.ExcelUtil;
import com.foo.common.core.page.TableDataInfo;

/**
 * 餐厅管理Controller
 *
 * @author foo
 * @date 2025-11-11
 */
@Controller
@RequestMapping("/wechat/WxCanteen")
public class WxCanteenController extends BaseController {
    private String prefix = "wechat/WxCanteen";

    @Autowired
    private IWxCanteenService wxCanteenService;
    @Autowired
    private IWxUserService wxUserService;

    @RequiresPermissions("wechat:WxCanteen:view")
    @GetMapping()
    public String WxCanteen() {
        return prefix + "/WxCanteen";
    }

    /**
     * 查询餐厅管理列表
     */
    @RequiresPermissions("wechat:WxCanteen:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxCanteen wxCanteen) {
        startPage();
        List<WxCanteen> list = wxCanteenService.selectWxCanteenList(wxCanteen);
        return getDataTable(list);
    }


    @GetMapping("/listAjax")
    @ResponseBody
    public AjaxResult listAjax(WxCanteen wxCanteen)
    {
        List<WxCanteen> list = wxCanteenService.selectWxCanteenList(wxCanteen);
        return AjaxResult.success (list);
    }

    /**
     * 新增餐厅管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存餐厅管理
     */
    @RequiresPermissions("wechat:WxCanteen:add")
    @Log(title = "餐厅管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxCanteen wxCanteen) {
        WxUser wxUser = wxUserService.selectWxUserById(wxCanteen.getCanteenUid());
        List<String> list = new ArrayList<>(Arrays.asList(wxUser.getUserType().split(",")));
        String target = "2";
        if (!list.contains(target)) {
            list.add(target);
            wxUser.setUserType(String.join(",", list));
            wxUserService.updateWxUser(wxUser);
        } else {
            return error("该用户已有绑定的餐厅！");
        }
        return toAjax(wxCanteenService.insertWxCanteen(wxCanteen));
    }

    /**
     * 修改餐厅管理
     */
    @GetMapping("/edit/{canteenId}")
    public String edit(@PathVariable("canteenId") Long canteenId, ModelMap mmap) {
        WxCanteen wxCanteen = wxCanteenService.selectWxCanteenById(canteenId);
        mmap.put("wxCanteen", wxCanteen);
        return prefix + "/edit";
    }

    /**
     * 修改保存餐厅管理
     */
    @RequiresPermissions("wechat:WxCanteen:edit")
    @Log(title = "餐厅管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxCanteen wxCanteen) {
        WxCanteen wxCanteen1 = wxCanteenService.selectWxCanteenById(wxCanteen.getCanteenId());
        if (!wxCanteen1.getCanteenUid().equals(wxCanteen.getCanteenUid())) {
            WxUser wxUser1 = wxUserService.selectWxUserById(wxCanteen.getCanteenUid());
            List<String> list = new ArrayList<>(Arrays.asList(wxUser1.getUserType().split(",")));
            String target = "2";
            if (!list.contains(target)) {
                list.add(target);
                wxUser1.setUserType(String.join(",", list));
                wxUserService.updateWxUser(wxUser1);
                WxUser wxUser2 = wxUserService.selectWxUserById(wxCanteen1.getCanteenUid());
                List<String> list1 = new ArrayList<>(Arrays.asList(wxUser2.getUserType().split(",")));
                list1.remove("2");
                wxUser2.setUserType(String.join(",", list1));
                wxUserService.updateWxUser(wxUser2);
            } else {
                return error("该用户已有绑定的餐厅！");
            }
        }


        return toAjax(wxCanteenService.updateWxCanteen(wxCanteen));
    }

    /**
     * 删除餐厅管理
     */
    @RequiresPermissions("wechat:WxCanteen:remove")
    @Log(title = "餐厅管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        WxCanteen wxCanteen = wxCanteenService.selectWxCanteenById(Long.parseLong(ids));
        WxUser wxUser = wxUserService.selectWxUserById(wxCanteen.getCanteenUid());
        List<String> list1 = new ArrayList<>(Arrays.asList(wxUser.getUserType().split(",")));
        list1.remove("2");
        wxUser.setUserType(String.join(",", list1));
        wxUserService.updateWxUser(wxUser);

        return toAjax(wxCanteenService.deleteWxCanteenByIds(ids));
    }
}
